package org.kpy;

import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(6666);
        Socket socket=serverSocket.accept();
        //读数据
        InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        String str=null;
        while((str=br.readLine())!=null) {
            //将收到的数据转换为JSON格式
            JSONObject object=JSONObject.parseObject(str);
            System.out.println("JSON："+object);
            System.out.println("服务器名称为："+object.getString("name")+"，端口号为："+object.getInteger("port"));
        }

        OutputStream out=socket.getOutputStream();
        OutputStreamWriter osw=new OutputStreamWriter(out);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write("Hello Client,I'm Server");
        bw.flush();
        socket.shutdownOutput();

        br.close();
        bw.close();
        socket.close();
    }
}
